const nodemailer = require("nodemailer");

const transporter = nodemailer.createTransport({
    host: "smtp.163.com",
    port: 465,
    secure: true, // true for port 465, false for other ports
    auth: {
        user: "jia9996958185@163.com",
        pass: "GEnih9MN7Uqm3gr5", // 换成自己的
    },
});

// verify connection configuration
transporter.verify(function (error, success) {
    if (error) {
        console.log(error);
    } else {
        console.log("Server is ready to take our messages");
    }
});

// async..await is not allowed in global scope, must use a wrapper
async function main(emailcode, title, message) {
    // send mail with defined transport object
    const info = await transporter.sendMail({
        from: '"小鲸鱼救助中心" <jia9996958185@163.com>', // sender address
        to: emailcode, // list of receivers
        subject: title, // Subject line
        // text: "今晚吃鸡腿吗", // plain text body
        html: message, // html body
    });

    console.log("Message sent", info);
    console.log("Message sent: %s", info.messageId);
    // Message sent: <d786aa62-4e0a-070a-47ed-0b0666549519@ethereal.email>
}


// main().catch(console.error);
exports.sendMail = main;